package dell.JDBCUtils;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JDBCUtils {
    //获取连接
    public static Connection getConnection() throws Exception {
        //1.读取配置文件中的4个基本信息
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
            System.out.println(is);
            Properties pros = new Properties();
            pros.load(is);
            String user=pros.getProperty("user");
            String password=pros.getProperty("password");
            String url=pros.getProperty("url");
            String driverClass=pros.getProperty("driverClass");
            //2.加载驱动
            Class.forName(driverClass);
            //3.获取连接
             Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println(connection);
        return connection;
    }
    //关闭资源(关闭连接可Statement)
    public static void closeResource(Connection connection, Statement ps){
        try {
            //7.资源的关闭
            if (ps!=null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection!=null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //关闭资源(关闭连接可Statement)
    public static void closeResource(Connection connection, Statement ps,ResultSet rs){
        //重载方法，需要对象的结果集时，对象的结果集也需要关闭
        try {
            //7.资源的关闭
            if (ps != null)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection!=null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (rs!=null)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
